<template>
  <div class="container">
    <el-card shadow="always">
      <!-- 搜索 -->
      <div>
        <el-row>
          <!-- 搜索框 -->
          <el-form inline ref="subjectFrom" :model="queryList">
            <el-form-item label="关键字" label-width="60px">
              <el-input
                class="inputCol"
                v-model="queryList.keyword"
                placeholder="请根据文章标题搜索"
              ></el-input>
            </el-form-item>
            <el-form-item label="状态" label-width="40px">
              <el-select
                v-model="queryList.state"
                placeholder="请选择"
                label-width="10px"
              >
                <el-option label="已启用" :value="1"> </el-option>
                <el-option label="已禁用" :value="0"> </el-option>
              </el-select>
            </el-form-item>

            <el-button size="small" class="colInput" plain @click="resetInput"
              >清除</el-button
            >
            <el-button
              size="small"
              class="colInput"
              type="primary"
              @click="handleFilter"
              >搜索</el-button
            >
            <el-button
              style="float: right"
              class="colright"
              size="mini"
              type="success"
              icon="el-icon-edit"
              @click="addDialogVisible = true"
              >新增技巧</el-button
            >
          </el-form>
        </el-row>
      </div>
      <el-alert type="info" class="alert" :closable="false" show-icon
        >共 {{ totally }} 条记录</el-alert
      >
      <!-- end -->
      <!-- 数据 -->
      <!-- v-loading="listLoading" -->
      <!-- :row-class-name="rowClassStatus" -->
      <el-table
        element-loading-text="给我一点时间"
        :key="tableKey"
        :data="dataList"
        fit
        highlight-current-row
      >
        <el-table-column sortable="" align="left" label="序号" type="index">
        </el-table-column>
        <el-table-column sortable="" align="left" prop="title" label="文章标题">
          <template slot-scope="scope">
            <span>{{ scope.row.title }}</span>
            <i
              @click="video(scope.row.videoURL)"
              v-if="scope.row.videoURL"
              class="el-icon-video-play"
              style="color='red'"
              aria-hidden="true"
            ></i>
          </template>
        </el-table-column>
        <el-table-column
          sortable=""
          align="center"
          label="阅读数"
          prop="visits"
        >
        </el-table-column>
        <el-table-column
          sortable=""
          align="left"
          prop="username"
          label="录入人"
        >
        </el-table-column>
        <el-table-column sortable="" align="left" label="录入时间">
          <!-- prop="createTime" -->
          <template slot-scope="{ row }">{{
            row.createTime | parseTime
          }}</template>
        </el-table-column>
        <el-table-column
          sortable=""
          align="center"
          :formatter="formatEmployment"
          prop="state"
          label="状态"
        >
        </el-table-column>
        <!-- 按钮 -->
        <el-table-column
          align="center"
          label="操作"
          class-name="small-padding "
        >
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="detail(scope.row)"
              >预览</el-button
            >
            {{ scope.row.state }}
            <!-- :disabled="scope.row.state === 0" -->
            <el-button @click="stopFn(scope.row)" type="text" size="small">{{
              scope.row.state === 0 ? "启用" : "禁用"
            }}</el-button>
            <el-button
              type="text"
              size="small"
              :disabled="scope.row.state === 1"
              @click="editArt(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="small"
              type="text"
              :disabled="scope.row.state === 1"
              @click="removeUser(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- end -->
      <!-- 分页 -->
      <div
        class="pagination"
        type="flex"
        justify="center"
        align="middle"
        style="height: 60px"
      >
        <PageTool
          :paginationPage="queryList.page"
          :paginationPagesize="queryList.pagesize"
          :total="totally"
          @pageChange="handleCurrentChange"
          @pageSizeChange="handleSizeChange"
        ></PageTool>
      </div>
    </el-card>
    <!-- end -->
    <!-- 新增标签弹层 -->
    <el-dialog
      title="添加文章"
      :visible.sync="addDialogVisible"
      width="55%"
      @close="resetAddArt"
    >
      <el-form
        :model="articlesForm"
        :rules="articlesFormRules"
        ref="articlesFormRef"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-form-item label="文章标题" prop="title" class="elform">
          <el-input
            v-model="articlesForm.title"
            placeholder="请输入文章标签"
          ></el-input>
        </el-form-item>
        <el-form-item label="文章标题" prop="articleBody" class="elform">
          <quill-editor
            :options="editorOption"
            style="margin-left: 0px"
            v-model="articlesForm.articleBody"
            ref="myQuillEditor"
          >
          </quill-editor>
        </el-form-item>
        <el-form-item label="视频地址" class="elform">
          <el-input
            v-model="articlesForm.videoURL"
            placeholder="请输入视频地址"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addArticlesFrom">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改弹框 -->
    <el-dialog
      title="添加文章"
      :visible.sync="editDialogVisible"
      width="55%"
      @close="resetEditArt"
    >
      <!-- 表单区域 -->
      <el-form
        :model="editArticlesForm"
        :rules="articlesFormRules"
        ref="editArticlesFormRef"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-form-item label="文章标题" prop="title" class="elform">
          <el-input
            v-model="editArticlesForm.title"
            placeholder="请输入文章标签"
          ></el-input>
        </el-form-item>
        <el-form-item label="文章标题" prop="articleBody" class="elform">
          <quill-editor
            :options="editorOption"
            class="ql-editor"

            style="margin-left: 0px;"
            v-model="editArticlesForm.articleBody"
            ref="myQuillEditor"
          >
          </quill-editor>
        </el-form-item>
        <el-form-item label="视频地址" class="elform">
          <el-input
            v-model="editArticlesForm.videoURL"
            placeholder="请输入视频地址"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editArticlesFrom">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 详情弹层 -->
    <el-dialog title="预览文章" :visible.sync="detailloag" width="55%" center>
      <h2 v-text="context.title"></h2>
      <!-- <span v-text="context.createTime"> -->
      <span>
        <template>
          {{ artcileDate | parseTime }}
        </template>
      </span>
      <span v-text="context.username"></span>
      <i class="el-icon-view"></i>
      <span v-text="context.visits"></span>
      <div v-html="context.articleBody"></div>
      <span slot="footer" class="dialog-footer"> </span>
    </el-dialog>
    <!-- 视频  -->
     <div class="video" v-if="videoUrl">
      <div class="video-ico" @click="closeVideo"><span class="el-icon-close"></span></div>
      <div class="video-box">
        <video autoplay :src="videoUrl"></video>
      </div>
    </div>
  </div>
</template>

<script>
import {
  list,
  remove,
  detail,
  changeState,
  add,
  update
} from '@/api/hmmm/articles'
import baseApi from '@/api/base/baseApi'
// import { simple } from '@/api/base/permissions'
// import { list, remove, detail, update, add } from '@/api/base/users'
// import { remove, detail } from '@/api/base/users'

import PageTool from '../../module-manage/components/page-tool.vue'
// import AddDetails from '../../module-manage/components/add_details.vue'
// import { parseTime } from '@/filters'
import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.snow.css'

export default {
  name: 'base-users',
  components: {
    quillEditor,
    PageTool
    // AddDetails
  },

  data () {
    return {
      editorOption: {
        // 工具菜单栏配置
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], // 加粗 斜体 下划线 删除线
            // ['blockquote', 'code-block'], // 引用 代码块
            // [{ list: 'ordered' }, { list: 'bullet' }], // 有序、无序列表
            // [{ script: 'sub' }, { script: 'super' }], // 上标/下标
            [{ align: [] }], // 对齐方式
            ['clean'], // 清除文本格式
            ['link', 'image', 'video'] // 链接、图片、视频
          ]
        }
      },
      totally: '',
      detailloag: false,
      context: {},
      dataList: [],
      addDialogVisible: false,
      editDialogVisible: false,
      videoUrl: null,
      artcileDate: '',
      stop: '',
      tableKey: 0,
      deletedDate: false,
      showDate: true,
      PermissionGroupsList: [], // 权限组加载
      total: null,
      listLoading: true,
      dialogStatus: '',
      queryList: {
        page: 1,
        pagesize: 5,
        keyword: '',
        state: null
      },

      // 添加框数据
      articlesForm: {
        title: '',
        articleBody: '',
        articlesInfo: [],
        videoURL: null
      },
      articlesFormRules: {
        title: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          {
            min: 3,
            max: 18,
            message: '长度在 3 到 18 个字符',
            trigger: 'blur'
          }
        ],
        articleBody: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]
      },
      editArticlesForm: {
        title: '',
        articleBody: '',
        videoURL: null,
        id: ''
      }
    }
  },
  computed: {},
  methods: {
    async list () {
      const { data } = await list(this.queryList)
      console.log(data)
      this.totally = data.counts // 总数
      // console.log(data.pagesize)
      this.dataList = data.items
      // console.log(data.items)
    },

    // 搜索信息
    handleFilter () {
      this.queryList.page = 1
      this.list(this.queryList)
      // this.list()
    },
    // 每页显示信息条数
    handleSizeChange (val) {
      this.queryList.pagesize = val
      if (this.queryList.page === 1) {
        // this.list(this.queryList)
        this.list(this.queryList)
      }
    },
    // 进入某一页
    handleCurrentChange (val) {
      this.queryList.page = val
      // console.log(val)
      this.list()
    },
    // 表格数据格式化
    formatEmployment (row, column, cellValue, index) {
      // 要去找 1所对应的值
      // console.log(baseApi.status)
      // console.log(cellValue)
      const obj = baseApi.status.find((item) => Number(item.id) === cellValue)
      return obj ? obj.value : '启用'
    },
    // 禁用
    async stopFn (str) {
      try {
        await changeState({
          id: str.id,
          state: str.state === 1 ? 0 : 1
        })
        // console.log(res)
        this.$message.success('操作成功!')
        str.state = str.state === 1 ? 0 : 1
      } catch (error) {
        this.$message.error('请求失败!')
      }
    },
    // 点击视频图标
    video (row) {
      this.videoUrl = row
    },
    closeVideo () {
      this.videoUrl = null
    },
    // 添加请求
    async addArticlesFrom () {
      try {
        await add(this.articlesForm)
        this.$message.success('新增文章成功')
        this.list()
        this.addDialogVisible = false
      } catch (e) {
        this.$message.error('新增文章失败')
      }
    },
    // 重置表单
    resetAddArt () {
      this.$refs.articlesFormRef.resetFields()
      this.articlesForm.articleBody = ''
      this.articlesForm.videoURL = ''
    },
    // 点击修改弹框显示
    editArt (row) {
      // console.log(row)
      this.editArticlesForm.title = row.title
      this.editArticlesForm.articleBody = row.articleBody
      this.editArticlesForm.videoURL = row.videoURL
      this.editArticlesForm.id = row.id
      this.editDialogVisible = true
    },
    // 修改的重置
    resetEditArt () {
      this.$refs.editArticlesFormRef.resetFields()
    },
    // 点击修改请求
    async editArticlesFrom () {
      try {
        await update(this.editArticlesForm)
        this.$message.success('修改文章成功')
        this.editDialogVisible = false
        this.list()
      } catch (e) {
        this.$message.error('修改文章失败' + 'e')
      }
    },
    // 详情信息
    async detail (data) {
      const res = await detail(data)
      // console.log(res.config.params)
      this.context = res.config.params
      this.artcileDate = res.config.params.createTime
      this.detailloag = true
    },
    // 删除
    removeUser (id) {
      this.$confirm('此操作将永久删除用户 ' + ', 是否继续?', '提示', {
        type: 'warning'
      })
        .then(() => {
          remove({ id })
            .then((res) => {
              this.$message.success('成功删除了用户' + 'res')
              this.dataList.splice(id, 1)
              // this.getList(this.requestParameters)
              this.list()
            })
            .catch((res) => {
              this.$message.error('删除失败!' + 'res')
            })
        })
        .catch(() => {
          this.$message.info('已取消操作!')
        })
    },
    // 点击清除按钮清除
    resetInput () {
      this.queryList.keyword = ''
      this.queryList.state = null
      this.list()
    }
  },
  created () {
    // 读取数据
    this.list()
  }
}
</script>

<style  lang='scss' scoped>
.ql-align-center {
  img {
    width: 700px;
  }
}

.preview-box {
  .preview-title {
    font-size: 25px;
    font-weight: 700;
    margin-bottom: 10px;
  }
  .preview-nav {
    span {
      margin-right: 10px;
    }
  }
  .preview-content {
    margin-top: 10px;
    padding: 10px;
    background-color: #f5f5f5;
    border-top: 1px dashed #ccc;
  }
}

.elform {
  width: 100%;
}

.el-form-item__content {
  margin-left: 10px;
}

.video {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
  overflow: hidden;
  z-index: 9999;
  .video-ico {
    width: 50px;
    height: 50px;
    position: absolute;
    top: 20px;
    left: 50%;
    transform: translate(-50%, 0);
    background: rgba(0, 0, 0, 0.4);
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.4);
    border-radius: 50%;
    text-align: center;
    line-height: 50px;
    color: #fff;
    font-size: 20px;
  }
  .video-box {
    width: 800px;
    height: 600px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -45%);
    video {
      width: 100%;
      height: 100%;
    }
  }
}

.left-fot {
  margin-left: 9px;
}
.el-row {
  margin-top: 10px;
}
.colInput {
  height: 35px;
}
.colright {
  float: right;
  height: 35px;
  margin-left: 10px;
}
.el-breadcrumb {
  padding-bottom: 20px;
  border-bottom: 1px solid #ccc;
}
.inputCol {
  width: 200px;
}
.el-form-item {
  display: inline-block;
}
.el-table {
  margin-top: 10px;
}
.el-pagination {
  float: right;
  margin-top: 20px;
}
.elform {
  width: 100%;
}

.el-form-item__content {
  margin-left: 10px;
}
.fa-film:before {
  color: #0245a8;
}
::v-deep .ql-editor {
 min-height: 200px;

}
</style>
